package com.dmitriyp.movie.editor;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;

import com.dmitriyp.movie.PluginResources;
import com.dmitriyp.movie.mapping.Format;
import com.dmitriyp.movie.mapping.Movie;
import com.dmitriyp.movie.ui.WidgetFactory;

public class MovieEditPage {

	private final static String ADD_STAR_BUTTON = "editor.button.add_star";//$NON-NLS-1$
	private final static String DELETE_STAR_BUTTON = "editor.button.delete_star";//$NON-NLS-1$
	private final static String ADD_STAR_DIALOG_TITLE = "dialog.title";//$NON-NLS-1$
	private final static String ADD_STAR_DIALOG_MESSAGE = "dialog.message";//$NON-NLS-1$

	public final static int MIN_YEAR = 1900;

	private ListViewer starsList;
	private Text titleField;
	private Spinner yearField;
	private ComboViewer formatField;
	private Button addButton, deleteButton;
	private Movie input;
	private InputDialog dialog;

	public MovieEditPage(Composite parent) {

		parent.setLayout(new GridLayout(3, false));

		Composite leftPart = new Composite(parent, 0);
		leftPart.setLayout(new GridLayout(2, false));
		leftPart.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true));

		titleField = WidgetFactory.createTextField(leftPart, "Title:");
		yearField = WidgetFactory.createSpinnerField(leftPart, "Year:");
		yearField.setValues(0, MIN_YEAR, 3000, 0, 1, 1);
		formatField = new ComboViewer(WidgetFactory.createComboField(leftPart,
				"Format:", SWT.READ_ONLY));
		formatField.setContentProvider(ArrayContentProvider.getInstance());
		formatField.setInput(Format.values());

		starsList = new ListViewer(parent, 0);
		starsList.getControl().setLayoutData(
				new GridData(SWT.FILL, SWT.FILL, true, true, 1, 2));
		starsList.setContentProvider(ArrayContentProvider.getInstance());

		Composite rightPart = new Composite(parent, 0);
		GridLayout layout = new GridLayout(1, false);
		layout.marginRight = 10;
		rightPart.setLayout(layout);
		rightPart.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, true));
		addButton = WidgetFactory.createButton(rightPart,
				PluginResources.getString(ADD_STAR_BUTTON), SWT.PUSH);
		deleteButton = WidgetFactory.createButton(rightPart,
				PluginResources.getString(DELETE_STAR_BUTTON), SWT.PUSH);
		GridData buttonData = new GridData(SWT.FILL, SWT.FILL, true, false);
		addButton.setLayoutData(buttonData);
		deleteButton.setLayoutData(buttonData);

		dialog = new InputDialog(parent.getShell(),
				PluginResources.getString(ADD_STAR_DIALOG_TITLE),
				PluginResources.getString(ADD_STAR_DIALOG_MESSAGE), "", null);

		addButton.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				if(dialog.open() == 0) {
					starsList.add(dialog.getValue());
				}
			}

		});
	}

	public void setValue(Movie movie) {
		this.input = movie;
	}

	public void refresh() {
		if (input == null)
			return;

		titleField.setText(input.getTitle() != null ? input.getTitle() : "");
		yearField.setSelection(input.getReleaseYear() != null ? input
				.getReleaseYear() : MIN_YEAR);
		formatField.setSelection(new StructuredSelection(
				input.getFormat() != null ? input.getFormat() : Format.VHS));
		starsList.setInput(input.getStars());
	}
	
	public void save() {
		if(input == null)
			return;
		input.setTitle(titleField.getText());
		input.setReleaseYear(yearField.getSelection());
		Format format = (Format) ((StructuredSelection)formatField.getSelection()).getFirstElement();
		input.setFormat(format);
		Set<String> stars = new HashSet<String>();
		Collections.addAll(stars, starsList.getList().getItems());
		input.setStars(stars);
	}
}
